//package com.zgjkhis.modules.charges.task;
//
//import cn.hutool.core.util.ArrayUtil;
//import com.alibaba.fastjson.JSONArray;
//import com.alibaba.fastjson.JSONObject;
//import com.zgjkhis.exception.ServiceException;
//import com.zgjkhis.modules.charges.domain.Logistics;
//import com.zgjkhis.modules.charges.domain.LogisticsRoute;
//import com.zgjkhis.modules.charges.service.LogisticsRouteService;
//import com.zgjkhis.modules.charges.service.LogisticsService;
//import com.zgjkhis.modules.charges.service.impl.LogisticsServiceImpl;
//import com.zgjkhis.modules.util.CharUtil;
//import com.zgjkhis.modules.util.HttpClient;
//import com.zgjkhis.modules.util.SHAUtils;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.beans.factory.annotation.Value;
//import org.springframework.scheduling.annotation.Scheduled;
//import org.springframework.stereotype.Component;
//
//import javax.annotation.Resource;
//import java.util.ArrayList;
//import java.util.HashMap;
//import java.util.List;
//import java.util.Map;
//
///**
// * @Author yxl 物流模块定时任务
// * @Description
// * @Date 2021/7/20 19:29
// **/
//@Component
//public class LogisticsScheduler {
//
//    @Value("${shunFen.base-url}")
//    private String baseUrl;
//    @Value("${shunFen.hospitalCode}")
//    private String hospitalCode;
//    @Value("${shunFen.secretKey}")
//    private String secretKey;
//
//    @Resource
//    private LogisticsService logisticsService;
//    @Resource
//    private LogisticsRouteService routeService;
//
//    private final static Logger logger = LoggerFactory.getLogger(LogisticsServiceImpl.class);
//
//    /**
//     *  查询物流
//     *  间隔2小时查询一次
//     */
//    @Scheduled(cron = "0 0 */2 * * ?")
//    public void queryLogistics() {
//        logger.info(String.format(".................... task.queryLogistics start"));
//        List<Logistics> list = this.logisticsService.queryNoComplete();
//        List<LogisticsRoute> routes;
//        if(!ArrayUtil.isEmpty(list)) {
//            for(Logistics item: list) {
//                routes = this.requestOrderRoute(item);
//                if(!ArrayUtil.isEmpty(routes)) {
//                    this.routeService.batchDelRoute(item.getBspOrderNo());
//                    this.routeService.saveBatch(routes);
//                    this.checkLogistics(item, routes);
//                }
//            }
//        }
//        logger.info(String.format(".................... task.queryLogistics end"));
//    }
//
//    private List<LogisticsRoute> requestOrderRoute(Logistics logistics) {
//        String orderNo = logistics.getBspOrderNo();
//        String sign = SHAUtils.SHA512(orderNo + secretKey);
//        String url = String.format("%s/api/open/api/listOrderRoute?hospitalCode=%s&sign=%s&orderNo=%s&version=1.0&timestamp=4070916000000", this.baseUrl, this.hospitalCode, sign, orderNo);
//
//        String result = HttpClient.doGet(url);
//        if(result == null) {
//            throw new ServiceException("系统错误");
//        }
//        JSONObject jSONObject = JSONObject.parseObject(result);
//        String message = jSONObject.getString("message");
//        if(!CharUtil.isEmpty(message)) {
//            throw new ServiceException(String.format("请求失败：%s", message));
//        }
//        JSONArray array = jSONObject.getJSONArray("result");
//        List<LogisticsRoute> routes = new ArrayList<>();
//        if(array.size() > 0) {
//            array.forEach(item -> {
//                routes.add(new LogisticsRoute((JSONObject) item, logistics));
//            });
//        }
//        return routes;
//    }
//
//    private void checkLogistics(Logistics logistics, List<LogisticsRoute> routes) {
//        for(LogisticsRoute route: routes) {
//            int code = route.getOpcode().intValue();
//            // 80或8000已签收
//            if(code == 80 || code == 8000) {
//                logger.info(String.format("已签收，bspOrderNo: %s", logistics.getBspOrderNo()));
//                logistics.setStatus(1);
//                this.logisticsService.updateById(logistics);
//                break;
//            }
//        }
//    }
//
//}
